package com.originskyseed.platform.course.controller;

import java.util.Arrays;
import java.util.Date;

import javax.servlet.http.HttpServletRequest;

import org.apache.dubbo.config.annotation.DubboReference;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.originskyseed.platform.core.aspect.annotation.RequiredParams;
import com.originskyseed.platform.core.base.BaseController;
import com.originskyseed.platform.core.base.BaseLocaleResource;
import com.originskyseed.platform.core.base.BasePage;
import com.originskyseed.platform.core.base.BaseResponseBody;
import com.originskyseed.platform.core.common.code.CommonResponseCode;
import com.originskyseed.platform.core.filter.helper.SingleSignOnTokenHelper;
import com.originskyseed.platform.course.i18n.CourseResponseMsg;
import com.originskyseed.platform.course.model.CourseRelateEvaluate;
import com.originskyseed.platform.course.service.ICourseRelateEvaluateService;
import com.originskyseed.platform.course.vo.request.CourseRelateEvaluateRequestVO;
import com.originskyseed.platform.user.model.UserInfo;

/**
 * 课程关联评价表Controller类（OSI_COURSE_RELATE_EVALUATE）
 * 
 * @author Tangwanghui
 */
@Controller
@RequestMapping("/courseRelateEvaluate")
@BaseLocaleResource(localeFileName = "i18n_course")
public class CourseRelateEvaluateController extends BaseController {

	private static final Logger logger = LoggerFactory.getLogger(CourseRelateEvaluateController.class);

	@DubboReference
	private ICourseRelateEvaluateService courseRelateEvaluateService;

	/**
	 * 分页查询
	 * 
	 * @param request
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/courseRelateEvaluateList.json", method = RequestMethod.POST)
	@RequiredParams(names = { "pageNo", "pageSize" })
	public BaseResponseBody<BasePage<CourseRelateEvaluate>> courseRelateEvaluateList(HttpServletRequest request) {
		BaseResponseBody<BasePage<CourseRelateEvaluate>> result = null;
		try {
			// 请求对象
			CourseRelateEvaluateRequestVO requestVO = this.getParameterBean(CourseRelateEvaluateRequestVO.class);

			// 分页查询
			BasePage<CourseRelateEvaluate> page = new BasePage<CourseRelateEvaluate>(requestVO.getPageNo(), requestVO.getPageSize());
			page = this.courseRelateEvaluateService.selectListByPage(requestVO.toMap(), page);
			result = new BaseResponseBody<BasePage<CourseRelateEvaluate>>(true, CommonResponseCode.SUCCESS.getCode(), this.getLocaleMessage(this.getClass(), CourseResponseMsg.DATA_SUCCESS.getMsg()), page);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			result = new BaseResponseBody<BasePage<CourseRelateEvaluate>>(false, CommonResponseCode.ERROR.getCode(), this.getLocaleMessage(this.getClass(), CourseResponseMsg.DATA_ERROR.getMsg()), e.getMessage(), null);
		}
		return result;
	}

	/**
	 * 保存信息
	 * 
	 * @param request
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/courseRelateEvaluateSave.json")
	public BaseResponseBody<CourseRelateEvaluate> courseRelateEvaluateSave(HttpServletRequest request) {
		BaseResponseBody<CourseRelateEvaluate> result = null;
		try {
			UserInfo userInfo = SingleSignOnTokenHelper.getCurrentUserInfo(UserInfo.class);
			// 请求对象
			CourseRelateEvaluate courseRelateEvaluate = this.getParameterBean(CourseRelateEvaluate.class);
			String taskCourseUserId = this.getParameterString("taskCourseUserId", null);

			courseRelateEvaluate.setCreator(userInfo.getName());
			courseRelateEvaluate.setCreatorId(userInfo.getId());

			this.courseRelateEvaluateService.insertCourseRelateEvaluate(courseRelateEvaluate, taskCourseUserId);
			result = new BaseResponseBody<CourseRelateEvaluate>(true, CommonResponseCode.SUCCESS.getCode(), this.getLocaleMessage(this.getClass(), CourseResponseMsg.SAVE_SUCCESS.getMsg()), courseRelateEvaluate);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			result = new BaseResponseBody<CourseRelateEvaluate>(false, CommonResponseCode.ERROR.getCode(), this.getLocaleMessage(this.getClass(), CourseResponseMsg.SAVE_ERROR.getMsg()), e.getMessage(), null);
		}
		return result;
	}

	/**
	 * 删除信息
	 * 
	 * @param request
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/courseRelateEvaluateDelete.json", method = RequestMethod.POST)
	@RequiredParams(names = { "ids" })
	public BaseResponseBody<Boolean> courseRelateEvaluateDelete(HttpServletRequest request) {
		BaseResponseBody<Boolean> result = null;
		try {
			// 请求参数
			String[] ids = this.getParameterArray("ids", new String[] {});

			this.courseRelateEvaluateService.deleteByPrimaryKeys(Arrays.asList(ids));
			result = new BaseResponseBody<Boolean>(true, CommonResponseCode.SUCCESS.getCode(), this.getLocaleMessage(this.getClass(), CourseResponseMsg.DELETE_SUCCESS.getMsg()), true);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			result = new BaseResponseBody<Boolean>(false, CommonResponseCode.ERROR.getCode(), this.getLocaleMessage(this.getClass(), CourseResponseMsg.DELETE_ERROR.getMsg()), e.getMessage(), null);
		}
		return result;
	}

	/**
	 * 根据id查询详情
	 *
	 * @param request
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/selectCourseRelateEvaluateById.json", method = RequestMethod.POST)
	@RequiredParams(names = { "id" })
	public BaseResponseBody selectCourseRelateEvaluateById(HttpServletRequest request) {
		BaseResponseBody result = null;
		try {
			// 请求参数
			String id = this.getParameterString("id", null);

			CourseRelateEvaluate courseRelateEvaluate = this.courseRelateEvaluateService.selectByPrimaryKey(id);
			result = new BaseResponseBody(true, CommonResponseCode.SUCCESS.getCode(), this.getLocaleMessage(this.getClass(), CourseResponseMsg.DELETE_SUCCESS.getMsg()), courseRelateEvaluate);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			result = new BaseResponseBody(false, CommonResponseCode.ERROR.getCode(), this.getLocaleMessage(this.getClass(), CourseResponseMsg.DELETE_ERROR.getMsg()), e.getMessage(), null);
		}
		return result;
	}

	/**
	 * 回复课程评价
	 *
	 * @param request
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/replyCourseRelateEvaluate.json", method = RequestMethod.POST)
	@RequiredParams(names = { "id" })
	public BaseResponseBody<Boolean> replyCourseRelateEvaluate(HttpServletRequest request) {
		BaseResponseBody result = null;
		try {
			// 当前登陆人信息
			UserInfo userInfo = SingleSignOnTokenHelper.getCurrentUserInfo(UserInfo.class);

			// 请求参数
			String id = this.getParameterString("id", null);
			String replyContent = this.getParameterString("replyContent", null);
			CourseRelateEvaluate courseRelateEvaluate = new CourseRelateEvaluate();
			courseRelateEvaluate.setId(id);
			courseRelateEvaluate.setReplyContent(replyContent);
			courseRelateEvaluate.setReplyTime(new Date());
			courseRelateEvaluate.setReplyId(userInfo.getId());
			courseRelateEvaluate.setReplyName(userInfo.getName());
			this.courseRelateEvaluateService.updateByPrimaryKeySelective(courseRelateEvaluate);

			result = new BaseResponseBody(true, CommonResponseCode.SUCCESS.getCode(), this.getLocaleMessage(this.getClass(), CourseResponseMsg.OPERATION_SUCCESS.getMsg()), courseRelateEvaluate);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			result = new BaseResponseBody(false, CommonResponseCode.ERROR.getCode(), this.getLocaleMessage(this.getClass(), CourseResponseMsg.OPERATION_ERROR.getMsg()), e.getMessage(), null);
		}
		return result;
	}
}